package com.burstly.lib.currency.request;

import com.burstly.lib.currency.BalanceUpdateInfo;
import com.burstly.lib.currency.UserAccount;
import com.burstly.lib.currency.UserAccountStorage;
import com.burstly.lib.currency.UserCurrencyAccount;
import com.burstly.lib.feature.currency.ICurrencyListener;
import com.burstly.lib.network.request.DefaultRequestCallback;
import com.burstly.lib.util.LoggerExt;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

class UpdateCurrencyCallback extends DefaultRequestCallback<AccountInfoResponse>
{
  static final LoggerExt LOG = LoggerExt.getInstance();
  static final String TAG = UpdateCurrencyCallback.class.getSimpleName();
  private final List<ICurrencyListener> mCurrencyListeners;
  private final UserAccount mUserAccount;

  UpdateCurrencyCallback(List<ICurrencyListener> paramList, UserAccount paramUserAccount)
  {
    this.mCurrencyListeners = paramList;
    this.mUserAccount = paramUserAccount;
  }

  private RequestResult createRequestResult(AccountInfoResponse paramAccountInfoResponse)
  {
    RequestResult localRequestResult = null;
    if (paramAccountInfoResponse != null)
    {
      boolean bool = "0".equals(paramAccountInfoResponse.getStatusCode());
      localRequestResult = null;
      if (bool)
      {
        AccountInfoResponse.AccountBean[] arrayOfAccountBean = paramAccountInfoResponse.getAccounts();
        if ((arrayOfAccountBean != null) && (arrayOfAccountBean.length != 0))
        {
          int i = arrayOfAccountBean.length;
          int j = 0;
          if (j < i)
          {
            AccountInfoResponse.AccountBean localAccountBean = arrayOfAccountBean[j];
            String str = localAccountBean.getCurrency();
            UserCurrencyAccount localUserCurrencyAccount1 = this.mUserAccount.getCurrencyAccount(str);
            if (localUserCurrencyAccount1 == null)
            {
              UserCurrencyAccount localUserCurrencyAccount2 = new UserCurrencyAccount(str);
              localUserCurrencyAccount2.setTotalCurrencyAmount(localAccountBean.getBalance());
              this.mUserAccount.setCurrencyAccount(str, localUserCurrencyAccount2);
            }
            while (true)
            {
              UserAccountStorage.getInstance().saveAccount(this.mUserAccount);
              j++;
              break;
              localUserCurrencyAccount1.setOldTotalCurrencyAmount(localUserCurrencyAccount1.getActualBalance());
              localUserCurrencyAccount1.setTotalCurrencyAmount(localAccountBean.getBalance());
            }
          }
        }
        else
        {
          LOG.logWarning(TAG, "Server returned no accounts", new Object[0]);
        }
        localRequestResult = succeeded();
      }
    }
    if (localRequestResult == null)
      localRequestResult = failed();
    return localRequestResult;
  }

  private RequestResult failed()
  {
    RequestResult localRequestResult = succeeded();
    localRequestResult.mSucceeded = false;
    return localRequestResult;
  }

  private void processResult(AccountInfoResponse paramAccountInfoResponse)
  {
    RequestResult localRequestResult = createRequestResult(paramAccountInfoResponse);
    if (!this.mCurrencyListeners.isEmpty());
    while (true)
    {
      ICurrencyListener localICurrencyListener;
      try
      {
        Iterator localIterator = this.mCurrencyListeners.iterator();
        if (localIterator.hasNext())
        {
          localICurrencyListener = (ICurrencyListener)localIterator.next();
          if (!localRequestResult.mSucceeded)
            break label84;
          localICurrencyListener.didUpdateBalance(localRequestResult.mUpdateEvent);
          continue;
        }
      }
      catch (Exception localException)
      {
        LOG.logThrowable(TAG, localException);
      }
      return;
      label84: localICurrencyListener.didFailToUpdateBalance(localRequestResult.mUpdateEvent);
    }
  }

  private RequestResult succeeded()
  {
    RequestResult localRequestResult = new RequestResult(null);
    localRequestResult.mSucceeded = true;
    localRequestResult.mUpdateEvent = BalanceUpdateInfo.createFrom(this.mUserAccount.getAllCurrencyAccounts());
    return localRequestResult;
  }

  public void onFailInBackground(AccountInfoResponse paramAccountInfoResponse)
  {
    processResult(paramAccountInfoResponse);
  }

  public void onSuccessInBackground(AccountInfoResponse paramAccountInfoResponse)
  {
    processResult(paramAccountInfoResponse);
  }

  private static class RequestResult
  {
    boolean mSucceeded;
    Map<String, BalanceUpdateInfo> mUpdateEvent;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.currency.request.UpdateCurrencyCallback
 * JD-Core Version:    0.6.2
 */